from inc import * 
def Status(request):
    if (request.session.has_key('login_id')):
        login_t = loader.get_template('_status.html')
        c = Context({'CSS_ROOT':'default','NAME':request.session['login_name'],'ID':request.session['login_id']})
        return HttpResponse(login_t.render(c))
    else:
        return HttpResponseRedirect('/core/user/login');

def Register(request):
    u_data = UserCore()
    command_error = model_obj_builder(u_data,request.REQUEST,reg_handler)
    if command_error:
        register_t = loader.get_template('__register_fail.xml')
        c = Context({"ERROR":command_error})
        return HttpResponse(register_t.render(c),mimetype = "text/xml")
    else:
        try:
            user = UserCore.objects.get(email=u_data.email)
            c = Context({'MSG':'Email address already been used',"REQUEST":request.REQUEST})
            register_t = loader.get_template('__register_conflict.xml')
            return HttpResponse(register_t.render(c))
        except UserCore.DoesNotExist:
            try:
              u_data.full_clean()
              u_data.save()
              t = loader.get_template('__register_succ.xml')
              c = Context({'CSS_ROOT':'default','REQUEST':request.REQUEST})
              request.session['login_id'] = u_data.email
              request.session['login_name'] = u_data.name
              return HttpResponse(t.render(c),mimetype = "text/xml")
            except ValidationError, e:
              register_t = loader.get_template('__register_fail.xml')
              c = Context({'CSS_ROOT':'default','REQUEST':request.REQUEST,'ERROR':e.message_dict})
              return HttpResponse(register_t.render(c),mimetype = "text/xml")

def Logout(request):
    try:
      del request.session['login_id']
      del request.session['login_name']
      login_t = loader.get_template('_loginbar.html')
      c = Context({'CSS_ROOT':'default'})
      resp = HttpResponse(login_t.render(c))
      resp.set_cookie('status','CHANGE')
      return resp
    except KeyError:
      pass
    return HttpResponse('Logout');

@session_prefix      
def Login(request):
    command_error = {}
    try:
        name = request.REQUEST['name']
        if not login_validater['name'].match(name):
            raise InvalidError()
        password_raw = request.REQUEST['password']
        if not login_validater['password'].match(password_raw):
            raise InvalidError()
    except KeyError:
        command_error['KeyError'] = 'KeyError'
    except InvalidError:
        command_error['InvalidError'] = 'InvalidError'
    login_t = loader.get_template('__login.html')
    if command_error:
        login_t = loader.get_template('__login.html')
        c = Context({'CSS_ROOT':'default','ERROR':'INPUT_INVALIDATE'})
        return HttpResponse(login_t.render(c),mimetype = "text/xml")
    else:
        try:
            password = sha.new(password_raw).hexdigest()
            user = UserCore.objects.get(name=name,password=password)
            request.session['login_id'] = user.email
            request.session['login_name'] = user.name
            c = Context({'CSS_ROOT':'default','USER_NAME':user.name})
            resp = HttpResponse(login_t.render(c),mimetype = "text/xml")
            return resp
        except UserCore.DoesNotExist:
            c = Context({'CSS_ROOT':'default','ERROR':'PASSWORD_ERROR'})
            resp = HttpResponse(login_t.render(c),mimetype = "text/xml")
            #resp.set_cookie('status','CHANGE')
            return resp
